home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / makeindex / sortid.c < prev   
Encoding:
C/C++ Source or Header  |  1990-04-26  |  2.9 KB  |  184 lines

  1. /*
  2.  *
  3.  * Copyright (C) 1987     Pehong Chen    (phc@renoir.berkeley.edu)
  4.  * Computer Science Division
  5.  * University of California, Berkeley
  6.  *
  7.  */
  8.  
  9. #include    "mkind.h"
  10.  
  11. static    int    idx_gc;
  12.  
  13.  
  14. void
  15. sort_idx()
  16. {
  17.     MESSAGE("Sorting entries...", "");
  18.     idx_dc = idx_gc = 0;
  19.     qsort((char*)idx_key, (int)idx_gt, (int)sizeof(FIELD_PTR), compare);
  20.     MESSAGE("done (%d comparisons).\n", idx_gc);
  21. }
  22.  
  23.  
  24. static int
  25. compare(a, b)
  26.     FIELD_PTR    *a;
  27.     FIELD_PTR    *b;
  28. {
  29.     int        i;
  30.     int        dif;
  31.  
  32.     idx_gc++;
  33.     IDX_DOT(CMP_MAX);
  34.  
  35.     for (i = 0; i < FIELD_MAX; i++) {
  36.         /* compare the sort fields */
  37.         if ((dif = compare_one((*a)->sf[i], (*b)->sf[i])) != 0)
  38.             break;
  39.  
  40.         /* compare the actual fields */
  41.         if ((dif = compare_one((*a)->af[i], (*b)->af[i])) != 0)
  42.             break;
  43.     }
  44.  
  45.     /* both key aggregates are identical, compare page numbers */
  46.     if (i == FIELD_MAX)
  47.         dif = compare_page(a, b);
  48.  
  49.     return(dif);
  50. }
  51.  
  52. static int
  53. compare_one(x, y)
  54.     char        *x;
  55.     char        *y;
  56. {
  57.     int        m;
  58.     int        n;
  59.  
  60.     if ((x[0] == NULL) && (y[0] == NULL))
  61.         return (0);
  62.  
  63.     if (x[0] == NULL)
  64.         return (-1);
  65.  
  66.     if (y[0] == NULL)
  67.         return (1);
  68.  
  69.     m = group_type(x);
  70.     n = group_type(y);
  71.  
  72.     /* both pure digits */
  73.     if ((m >= 0) && (n >= 0))
  74.         return (m-n);
  75.  
  76.     /* x digit, y non-digit */
  77.     if (m >= 0)
  78.         return ((n == -1) ? 1 : -1);
  79.  
  80.     /* x non-digit, y digit */
  81.     if (n >= 0)
  82.         return ((m == -1) ? -1 : 1);
  83.  
  84.     /* strings started with a symbol (including digit) */
  85.     if ((m == SYMBOL) && (n == SYMBOL))
  86.         return (check_mixsym(x, y));
  87.  
  88.     /* x symbol, y non-symbol */
  89.     if (m == SYMBOL)
  90.         return (-1);
  91.  
  92.     /* x non-symbol, y symbol */
  93.     if (n == SYMBOL)
  94.         return (1);
  95.  
  96.     /* strings with a leading letter, the ALPHA type */
  97.     return (compare_string(x, y));
  98. }
  99.  
  100. static int
  101. check_mixsym(x, y)
  102.     char        *x;
  103.     char        *y;
  104. {
  105.     int        m;
  106.     int        n;
  107.  
  108.     m = ISDIGIT(x[0]);
  109.     n = ISDIGIT(y[0]);
  110.  
  111.     if (m && !n)
  112.         return(1);
  113.  
  114.     if (!m && n)
  115.         return (-1);
  116.  
  117.     return (strcmp(x, y));
  118. }
  119.  
  120.  
  121. static int
  122. compare_string(a, b)
  123.     char        *a;
  124.     char        *b;
  125. {
  126.     int        i = 0;
  127.     int        j = 0;
  128.     char        al;
  129.     char        bl;
  130.  
  131.     while ((a[i] != NULL) || (b[j] != NULL)) {
  132.         if (a[i] == NULL)
  133.             return (-1);
  134.         if (b[j] == NULL)
  135.             return (1);
  136.         if (letter_ordering) {
  137.             if (a[i] == SPC)
  138.                 i++;
  139.             if (b[j] == SPC)
  140.                 j++;
  141.         }
  142.         al = TOLOWER(a[i]);
  143.         bl = TOLOWER(b[j]);
  144.  
  145.         if (al != bl)
  146.             return (al - bl);
  147.         i++;
  148.         j++;
  149.     }
  150.     return (strcmp(a, b));
  151. }
  152.  
  153. static int
  154. compare_page(a, b)
  155.     FIELD_PTR    *a;
  156.     FIELD_PTR    *b;
  157. {
  158.     int        m;
  159.     short        i = 0;
  160.  
  161.     while ((i < (*a)->count) && (i < (*b)->count) &&
  162.            ((m = (*a)->npg[i] - (*b)->npg[i]) == 0)) {
  163.         i++;
  164.     }
  165.     if (m == 0) {
  166.         if ((i == (*a)->count) && (i == (*b)->count)) {
  167.             if (STREQ((*a)->encap, (*b)->encap))
  168.                 /* identical entries */
  169.                 (*b)->type = DUPLICATE;
  170.             else if ((*(*a)->encap == idx_ropen) ||
  171.                  (*(*b)->encap == idx_rclose))
  172.                 m = -1;
  173.             else if ((*(*a)->encap == idx_rclose) ||
  174.                  (*(*b)->encap == idx_ropen))
  175.                 m = 1;
  176.         } else if ((i == (*a)->count) && (i < (*b)->count))
  177.             m = -1;
  178.         else if ((i < (*a)->count) && (i == (*b)->count))
  179.             m = 1;
  180.     }
  181.  
  182.     return (m);
  183. }
  184.